package net.lab1024.sa.admin.module.business.erp.bas.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.erp.bas.dao.ApprovordDao;
import net.lab1024.sa.admin.module.business.erp.bas.dao.ApprovordaDao;
import net.lab1024.sa.admin.module.business.erp.bas.dao.ApptemordaDao;
import net.lab1024.sa.admin.module.business.erp.bas.dao.CalendarsDao;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.ApprovordEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.ApprovordaEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.entity.CalendarsEntity;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.ApprovordAddForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.ApprovordQueryForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.form.ApprovordUpdateForm;
import net.lab1024.sa.admin.module.business.erp.bas.domain.vo.ApprovordVO;
import net.lab1024.sa.admin.module.business.erp.mmo.dao.MmordDao;
import net.lab1024.sa.admin.module.business.erp.mmo.domain.entity.MmordEntity;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 审批主表 Service
 *
 * @Author AKITA
 * @Date 2025-04-23 14:10:37
 * @Copyright AKITA
 */

@Service
public class ApprovordService {

    @Resource
    private ApprovordDao approvordDao;
    @Resource
    private CalendarsDao calendarsDao;
    @Resource
    private ApptemordService apptemordService;
    @Resource
    private ApptemordaDao apptemordaDao;
    @Resource
    private ApprovordaDao approvordaDao;
    @Resource
    private MmordDao mmordDao;


    /**
     * 分页查询
     */
    public PageResult<ApprovordVO> queryPage(ApprovordQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<ApprovordVO> list = approvordDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    @Transactional
    public ResponseDTO<Long> add(ApprovordAddForm addForm) {


        return ResponseDTO.ok();

    }

    /**
     * 更新
     */

    @Transactional
    public ResponseDTO<String> update(ApprovordUpdateForm updateForm) {
        ApprovordEntity approvordEntity = SmartBeanUtil.copy(updateForm, ApprovordEntity.class);
        /*拒绝审核*/
        if ("X".equals(approvordEntity.getDocStatus())) {
            ApprovordaEntity approvordaEntity1 = approvordEntity.getLista().get(approvordEntity.getApprovLineNum() - 1);
            approvordaEntity1.setBeginDate(LocalDateTime.now());
            approvordaEntity1.setLineStatus("X");
            approvordaDao.update(approvordaEntity1, new LambdaQueryWrapper<ApprovordaEntity>().eq(true, ApprovordaEntity::getDocEntry, approvordaEntity1.getDocEntry()).eq(true, ApprovordaEntity::getLineNum, approvordaEntity1.getLineNum()));
            if (approvordDao.updateById(approvordEntity) == 0) {
                throw new RuntimeException("数据已被更新，刷新后重试！");
            }
            return ResponseDTO.ok();
        } else {
            ApprovordaEntity approvordaEntity = approvordEntity.getLista().get(approvordEntity.getApprovLineNum() - 1);
            approvordaEntity.setLineStatus("C");
            approvordaDao.update(approvordaEntity, new LambdaQueryWrapper<ApprovordaEntity>().eq(true, ApprovordaEntity::getDocEntry, approvordaEntity.getDocEntry()).eq(true, ApprovordaEntity::getLineNum, approvordaEntity.getLineNum()));
            ApprovordaEntity approvordaEntity1 = approvordEntity.getLista().get(approvordEntity.getApprovLineNum());
            approvordaEntity1.setBeginDate(LocalDateTime.now());

            /*最后一次审核 */
            if (updateForm.getApprovLineNum() == updateForm.getLista().size() - 1) {
                /* 审核*/
                if (updateForm.getTabName().equals("calendars")) {
                    if ("A".equals(updateForm.getApprovType())) {
                        CalendarsEntity calendarsEntity = new CalendarsEntity();
                        calendarsEntity.setDocEntry(updateForm.getBaseEntry());
                        calendarsEntity.setDocStatus("O");
                        calendarsEntity.setApproved(true);
                        calendarsDao.updateById(calendarsEntity);

                    } else if ("B".equals(updateForm.getApprovType())) {
                        CalendarsEntity calendarsEntity = new CalendarsEntity();
                        calendarsEntity.setDocEntry(updateForm.getBaseEntry());
                        calendarsEntity.setDocStatus("O");
                        calendarsEntity.setApproved(false);
                        calendarsDao.updateById(calendarsEntity);
                    }
                } else if (updateForm.getTabName().equals("mmord")) {
                    if ("A".equals(updateForm.getApprovType())) {

                        MmordEntity mmordEntity = new MmordEntity();
                        mmordEntity.setId(updateForm.getBaseEntry());
                        mmordEntity.setDocStatus("O");
                        mmordEntity.setApproved(true);
                        mmordDao.updateById(mmordEntity);

                    } else if ("B".equals(updateForm.getApprovType())) {
                        MmordEntity mmordEntity = new MmordEntity();
                        mmordEntity.setId(updateForm.getBaseEntry());
                        mmordEntity.setDocStatus("O");
                        mmordEntity.setApproved(false);
                        mmordDao.updateById(mmordEntity);
                    }
                }
                approvordaEntity1.setLineStatus("C");
                approvordEntity.setDocStatus("C");

            } else {
                approvordaEntity1.setLineStatus("L");
            }
            approvordaDao.update(approvordaEntity1, new LambdaQueryWrapper<ApprovordaEntity>().eq(true, ApprovordaEntity::getDocEntry, approvordaEntity1.getDocEntry()).eq(true, ApprovordaEntity::getLineNum, approvordaEntity1.getLineNum()));
            approvordEntity.setApprovLineNum(approvordEntity.getApprovLineNum() + 1);
            if (approvordDao.updateById(approvordEntity) == 0) {
                throw new RuntimeException("数据已被更新，刷新后重试！");
            }
            return ResponseDTO.ok();
        }
    }


    public ResponseDTO<String> batchDelete(List<Long> idList) {
        approvordDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    @Transactional
    public ResponseDTO<String> delete(Long docEntry) {
        ApprovordEntity approvordEntity = approvordDao.selectById(docEntry);

        if (approvordEntity.getTabName().equals("calendars")) {
            CalendarsEntity calendarsEntity = calendarsDao.selectById(approvordEntity.getBaseEntry());
            calendarsEntity.setDocStatus("O");
            if (calendarsDao.updateById(calendarsEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        } else if (approvordEntity.getTabName().equals("mmord")) {
            MmordEntity mmordEntity = mmordDao.selectById(approvordEntity.getBaseEntry());
            mmordEntity.setDocStatus("O");
            if (mmordDao.updateById(mmordEntity) == 0) {
                throw new RuntimeException("数据异常，刷新重试！");
            }
        }
        approvordDao.deleteById(docEntry);
        approvordaDao.deleteById(docEntry);

        return ResponseDTO.ok();
    }

    public List<ApprovordEntity> info(ApprovordQueryForm queryForm) {
        return approvordDao.info(queryForm.getTabName(), queryForm.getBaseEntry());
    }
}
